package autoteam.ui.dialogs.profiles;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import db.DBRecord;


public abstract class ProfilePanel extends JPanel
{
    private static final long serialVersionUID = 3035935463962075001L;

    protected Dimension panelSize = new Dimension(500,350);;
    
    protected final boolean noneditable = false;
    protected final boolean editable = true;
    
    public static final Font defaultFont = new Font( "sansserif", Font.BOLD, 12);
    // private Color defaultTextColor;

    protected final Border raisedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
    protected final Border loweredBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

    protected JLabel avatar;
    protected ImageIcon avatarIcon;
    
    protected JLabel biographyLabel;
    protected JTextArea biography;
    
    protected DBRecord record;
    
    public ProfilePanel( DBRecord profileRecord )
    {
	super();

	avatar = new JLabel();
	avatarIcon = new ImageIcon("./trunk/img/profile_default.gif");
	avatar.setIcon(avatarIcon);
	avatar.setBorder(raisedBorder);

	biographyLabel = new JLabel( "Biography: ");
	biography = new JTextArea( 5, 20 );

	this.setLayout(null);
	this.setOpaque(false);
	this.setPreferredSize(panelSize);
	
	this.setDefaultBounds();
	
	this.add( avatar );
	this.add(  biographyLabel );
	this.add( biography );
    }

    public void setDefaultBounds()
    {
	avatar.setBounds( 0, 10, 150, 150 );
	biographyLabel.setBounds( 0, 170, 150, 25 );
	biography.setBounds( 0, 200, 500, 150 );
    }
    
    public abstract void setBounds();
    
    
    /* change profile picture */
    public void changeProfilePic( String filepath, int width, int height )
    {
        avatarIcon = new ImageIcon( filepath );
        Image image = avatarIcon.getImage( );
        avatar.setIcon( resizeImage( image, width, height ) );
    }
    
    public void changeProfilePic( String filepath )
    {
        avatarIcon = new ImageIcon( filepath );
        Image image = avatarIcon.getImage( );
        avatar.setIcon( resizeImage(image,150,150) );
    }

    /* helper method to resize image */
    public ImageIcon resizeImage( Image image, int width, int height )
    {
        Image resizedImg = image.getScaledInstance( width, height, java.awt.Image.SCALE_SMOOTH );
        return new ImageIcon( resizedImg );
    }
    
    /* set profile holder biography */
    public void setBiography( String bioText, Font fontStyle, boolean editable )
    {
	biography.setFont( fontStyle );
	biography.setEditable( editable );
        biography.setLineWrap( true );
        biography.setWrapStyleWord( true );
        biography.setBorder( raisedBorder );
        biography.setText( bioText );
    }
    
    public void hideBiography( )
    {
        biography.setVisible( false );
    }
    
}